package com.ruoyi.buss.grid.sh.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.buss.customer.mapper.DataCustomerRecordMapper;
import com.ruoyi.buss.customer.mapper.DataWgJtcyMapper;
import com.ruoyi.buss.grade.domain.DataCustomQuery;
import com.ruoyi.buss.grade.mapper.DataCustomQueryMapper;
import com.ruoyi.buss.grid.sh.domain.DataGtgsh;
import com.ruoyi.buss.grid.sh.domain.DataWgShsurvey;
import com.ruoyi.buss.grid.sh.mapper.DataGtgshMapper;
import com.ruoyi.buss.grid.sh.service.IDataGtgshService;
import com.ruoyi.buss.group.domain.DataCustomerGroupRelation;
import com.ruoyi.buss.group.mapper.DataCustomerGroupRelationMapper;
import com.ruoyi.buss.report.mapper.ReportWgshLsbMapper;
import com.ruoyi.buss.task.domain.DataPlanTaskRelation;
import com.ruoyi.buss.task.domain.DataTaskInfo;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.mapper.DataPlanTaskRelationMapper;
import com.ruoyi.buss.task.mapper.DataTaskExRecordBackMapper;
import com.ruoyi.buss.task.mapper.DataTaskExRecordMapper;
import com.ruoyi.buss.task.mapper.DataUserTaskRelationMapper;
import com.ruoyi.buss.task.service.IDataTaskInfoService;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.model.LoginUser;
/**
 * 网格商户Service业务层处理
 * 
 * @author zjh
 * @date 2021-07-15
 */
@Service
public class DataGtgshServiceImpl implements IDataGtgshService {
	@Autowired
	private DataGtgshMapper dataGtgshMapper;

	@Autowired
	private DataTaskExRecordMapper dataTaskExRecordMapper;
	
	@Autowired
    private DataUserTaskRelationMapper dataUserTaskRelationMapper;
	
	@Autowired
	private DataTaskExRecordBackMapper dataTaskExRecordBackMapper;
	
	@Autowired
	private DataWgJtcyMapper dataWgJtcyMapper;
	
	@Autowired
	private DataCustomerRecordMapper dataCustomerRecordMapper;
	
	@Autowired
	private ReportWgshLsbMapper reportWgshLsbMapper;
	
	@Autowired
	private DataCustomQueryMapper dataCustomQueryMapper;
	@Autowired
	private DataCustomerGroupRelationMapper dataCustomerGroupRelationMapper;
    @Autowired
    private IDataTaskInfoService dataTaskInfoService;
    @Autowired
    private DataPlanTaskRelationMapper dataPlanTaskRelationMapper;
  //创建固定的线程池
  	private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(30);

	/**
	 * 支行商户列表
	 */
	@Override
	public List<DataGtgsh> dataWgShList(DataGtgsh dataGtgsh, LoginUser userInfo) {
		List<DataGtgsh> doList = new ArrayList<DataGtgsh>();
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			if (StringUtils.isNotEmpty(dataGtgsh.getOrgId())) {
				doList = dataGtgshMapper.SurveyShList(dataGtgsh);
			} else {
				doList = dataGtgshMapper.getSurveyList();
			}
		} else if (userInfo.getRoles().contains("zhhz") && StringUtils.isEmpty(dataGtgsh.getOrgId())) {
			dataGtgsh.setOrgId(orgId);
			doList = dataGtgshMapper.SurveyShList(dataGtgsh);
		} else if (userInfo.getRoles().contains("common")) {
			dataGtgsh.setOrgId(orgId);
			dataGtgsh.setUserId(userId);
			doList = dataGtgshMapper.SurveyShList(dataGtgsh);
		}
		return doList;
	}
	/**
	 * 支行商户概况
	 */
	@Override
	public DataWgShsurvey dataWgShsurvey(DataGtgsh dataGtgsh, LoginUser userInfo) {
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		DataWgShsurvey dataWgShsurvey = null;
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			if (StringUtils.isNotEmpty(dataGtgsh.getOrgId())) {
				dataWgShsurvey = dataGtgshMapper.getSurvey(dataGtgsh);
			} else {
				dataWgShsurvey = dataGtgshMapper.getSurvey(new DataGtgsh());
			}
		} else if (userInfo.getRoles().contains("zhhz") && StringUtils.isEmpty(dataGtgsh.getOrgId())) {
			dataGtgsh.setOrgId(orgId);
			dataWgShsurvey = dataGtgshMapper.getSurvey(dataGtgsh);
		} else if (userInfo.getRoles().contains("common")) {
			dataGtgsh.setOrgId(orgId);
			dataGtgsh.setUserId(userId);
			dataWgShsurvey = dataGtgshMapper.getSurvey(dataGtgsh);
		}
		return dataWgShsurvey;
	}
	/**
	 *  包片商户列表
	 */
	@Override
	public List<DataGtgsh> queryGtgshList(DataGtgsh dataGtgsh, LoginUser userInfo) {
		List<DataGtgsh> doList = new ArrayList<DataGtgsh>();
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {

		} else if (userInfo.getRoles().contains("zhhz") && StringUtils.isEmpty(dataGtgsh.getOrgId())) {
			dataGtgsh.setOrgId(orgId);
		} else if (userInfo.getRoles().contains("common")) {
			dataGtgsh.setOrgId(orgId);
			dataGtgsh.setUserId(userId);
		}
		
		doList = dataGtgshMapper.queryGtgshList(dataGtgsh);
		return doList;
		
	}
	
	/**
	 * 查询个体工商户
	 * @param idCard
	 */
	@Override
	public DataGtgsh itemGtgsh(String idCard) {
		List<DataGtgsh> gtgshList = dataGtgshMapper.selectOne(idCard);

		if (gtgshList != null && gtgshList.size()>0) {
			return gtgshList.get(0);
		}

		return new DataGtgsh();
	}
	
	/**
	 * 新增网格商户
	 * 
	 * @param dataGtgsh 网格商户
	 * @return 结果
	 */
	@Override
	public AjaxResult insertDataGtgsh(DataGtgsh dataGtgsh,LoginUser userInfo) {
		
		if (StringUtils.isEmpty(dataGtgsh.getZjhm())) {
			throw new CustomException("商户证件号码不能为空！");
	    }
		DataGtgsh dataWgsh = new DataGtgsh();
		dataWgsh.setZjhm(dataGtgsh.getZjhm());
		List<DataGtgsh> list = dataGtgshMapper.selectDataGtgshList(dataWgsh);
		if(list.size() > 0) {
			throw new CustomException("网格商户信息已存在！");
	    }
		
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		String id = GetOnlyId.getOnlyId();
	
		dataGtgsh.setId(id);
		dataGtgsh.setCreateUser(userId);
		dataGtgsh.setUpdateUser(userId);
		dataGtgsh.setCreateTime(DateUtils.getNowDate());
		
		if (StringUtils.isEmpty(dataGtgsh.getOrgId()) || StringUtils.isEmpty(dataGtgsh.getOrgId())) {
			// 如果没指定机构或者客户经理，默认当前用户为
			dataGtgsh.setOrgId(orgId);
			dataGtgsh.setUserId(userId);
		}
	
		try {
			if(StringUtils.isNotEmpty(dataGtgsh.getGroup())) {
				String[] qtbqArr = dataGtgsh.getGroup().split(",");
				if(qtbqArr!= null && qtbqArr.length>0) {
					DataCustomerGroupRelation group = new DataCustomerGroupRelation();
					group.setIdCard(dataGtgsh.getZjhm());
					group.setStatus("2");
					dataCustomerGroupRelationMapper.updateDataCustomerGroupRelation(null);
				}
				for(String qtbq:qtbqArr) {
					DataCustomerGroupRelation group = new DataCustomerGroupRelation();
					group.setIdCard(dataGtgsh.getZjhm());
					group.setKhqt(qtbq);
					group.setStatus("1");
					group.setCreateTime(DateUtils.getNowDate());
					group.setOrgId(orgId);
					group.setUserId(userId);
					dataCustomerGroupRelationMapper.insertDataCustomerGroupRelation(group);
				}
			}
			dataGtgshMapper.insertDataGtgsh(dataGtgsh);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("新增网格商户信息异常！");
		}
		
		//新增客户之后，调用存储过程，更新当天最新的商户概况
        new Thread(
				new Runnable(){  
		            public void run(){  
		            	dataGtgshMapper.callShPro();
		            }
			    }
		).start();  
		return AjaxResult.success();
	}
	
	/**
	 * 	删除个体工商户  逻辑删除
	 * @param gtgsh
	 * @return
	 */
	@Override
	public AjaxResult deleteGtgsh(DataGtgsh gtgsh) {
		try {
			dataGtgshMapper.deleteGtgsh(gtgsh);
		} catch (RuntimeException e) {
        	e.printStackTrace();
        	throw new CustomException("修改网格商户信息异常！");
        }
		return AjaxResult.success();
		
	}
	
	/**
	 * 修改网格商户
	 * 
	 * @param dataGtgsh 网格商户
	 * @return 结果
	 */
	@Override
	@Transactional
	public AjaxResult updateDataGtgsh(DataGtgsh dataGtgsh) {
		DataGtgsh dataWgsh = new DataGtgsh();
		dataWgsh.setZjhm(dataGtgsh.getZjhm());
		List<DataGtgsh> checkRet = dataGtgshMapper.selectDataGtgshList(dataGtgsh);
		dataWgsh.setZjhm(null);
		dataWgsh.setId(dataGtgsh.getId());
		List<DataGtgsh> oldList = dataGtgshMapper.selectDataGtgshList(dataGtgsh);
		DataGtgsh old = oldList.get(0);
		
		if (checkRet.size() > 0 && !checkRet.get(0).getId().equals(old.getId())) {
			throw new CustomException("修改的证件号码已存在，请重试！");
		}
		try {
			String idCard = old.getZjhm();
			String newIdCard = dataGtgsh.getZjhm();
			//根据ID_CARD修改相关表中的ID_CARD
			dataTaskExRecordMapper.updateDataTaskExRecordByIdCard(idCard,newIdCard);
			dataUserTaskRelationMapper.upUserTaskIdCardByIdCard(idCard,newIdCard);
			dataTaskExRecordBackMapper.upTaskExRecordBackIdCardByIdCard(idCard,newIdCard);
			dataWgJtcyMapper.upJtcyIdCardByIdCard(idCard,newIdCard);
			
			dataGtgsh.setUpdateTime(DateUtils.getNowDate());
			
			if(StringUtils.isNotEmpty(dataGtgsh.getGroup())) {
				String[] qtbqArr = dataGtgsh.getGroup().split(",");
				if(qtbqArr!= null && qtbqArr.length>0) {
					DataCustomerGroupRelation group = new DataCustomerGroupRelation();
					group.setIdCard(dataGtgsh.getZjhm());
					group.setStatus("2");
					dataCustomerGroupRelationMapper.updateDataCustomerGroupRelation(null);
				}
				for(String qtbq:qtbqArr) {
					DataCustomerGroupRelation group = new DataCustomerGroupRelation();
					group.setIdCard(dataGtgsh.getZjhm());
					group.setKhqt(qtbq);
					group.setStatus("1");
					group.setCreateTime(DateUtils.getNowDate());
					dataCustomerGroupRelationMapper.insertDataCustomerGroupRelation(group);
				}
			}
			//修改商户信息
			dataGtgshMapper.updateDataGtgsh(dataGtgsh);
			//保存修改记录
	        this.addUpdateRecord(old, dataGtgsh);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("修改网格商户相关信息异常！");
		}
		return AjaxResult.success();
	}
	@Override
	@Transactional
	public AjaxResult updateGtgshOrg(DataGtgsh dataGtgsh, LoginUser userInfo) {
		
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			try {
				dataGtgshMapper.updateGtgshOrg(dataGtgsh);
				reportWgshLsbMapper.updateGtgshLsb(dataGtgsh);
			} catch (Exception e) {
				e.printStackTrace();
				throw new CustomException("修改网格商户相关管户异常！"+e.getMessage());
			}
		} else {
			return AjaxResult.error("没有权限执行此操作！");
		}
		
		return AjaxResult.success();
	}
	@Override
	@Transactional
	public AjaxResult shTask(DataGtgsh dataGtgsh, LoginUser userInfo) {
		
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();

		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			
		} else if (userInfo.getRoles().contains("zhhz") && StringUtils.isEmpty(dataGtgsh.getOrgId())) {
			dataGtgsh.setOrgId(orgId);
		} else if (userInfo.getRoles().contains("common")) {
			dataGtgsh.setOrgId(orgId);
			dataGtgsh.setUserId(userId);
		}else {
			return AjaxResult.error("没有权限执行此操作！");
		}
		
		List<DataGtgsh> doList = dataGtgshMapper.queryGtgshList(dataGtgsh);
		
		if(StringUtils.isEmpty(doList)) {
			return AjaxResult.error("未查询到满足条件的客户！");
		}
		/**
		 * 先保存方案
		 */
	    String planCode = GetOnlyId.getOnlyId();
        DataCustomQuery dataCustomQuery = new DataCustomQuery();
        dataCustomQuery.setPlanCode(planCode);
        dataCustomQuery.setPlanName(dataGtgsh.getPlanName());
        dataCustomQuery.setCreateUser(userId);
        dataCustomQuery.setCreateTime(DateUtils.getNowDate());
        dataCustomQueryMapper.insertDataCustomQuery(dataCustomQuery);
        
        DataTaskInfo dataTaskInfo = new DataTaskInfo();        
        dataTaskInfo.setTaskType("1");
        dataTaskInfo.setIfGroup("0");
        dataTaskInfo.setGroupCode(planCode);
       
		if (dataGtgsh.getBeginDate() != null && dataGtgsh.getEndDate() != null) {
			dataTaskInfo.setBeginDate(DateUtilss.beginOfDate(dataGtgsh.getBeginDate()));// 设置任务开始时间
			dataTaskInfo.setEndDate(DateUtilss.endOfDate(dataGtgsh.getEndDate())); // 设置任务结束时间
		} else {
			dataTaskInfo.setBeginDate(DateUtilss.beginOfDate(new Date()));// 设置任务开始时间
			dataTaskInfo.setEndDate(DateUtilss.endOfDate(new Date())); // 设置任务结束时间
		}		
		dataTaskInfo.setTaskName(dataGtgsh.getPlanName());
		dataTaskInfo.setTaskTarget("CK,DK,ETC,XYK,SJYH,WY,DXQY,SDDJ,SYB,POS");
		Map<String,String> code = dataTaskInfoService.insertTask(dataTaskInfo,userInfo);
        if( code.get("taskId")!=null ) {
        	/**
        	 * 另启一个线程池
        	 * 营销任务生成成功，则将查询方案和方案的客户进行关联
        	 * 需要设置DATA_PLAN_TASK_RELATION status为0  不加入定时任务自动关联方案与客户
        	 */
            fixedThreadPool.execute(() -> {
                try {
                    final List<DataGtgsh> shList = dataGtgshMapper.SurveyShList(dataGtgsh);
                    
                	String taskId = code.get("taskId");
                	
                	DataPlanTaskRelation planTaskVO = new DataPlanTaskRelation();
                	planTaskVO.setPlanCode(planCode);
                	planTaskVO.setTaskId(taskId);
                	
                	dataPlanTaskRelationMapper.updateDataPlanTaskRelation(planTaskVO);
                	
                	List<DataUserTaskRelation> relationList = new ArrayList<DataUserTaskRelation>();
                	//关联方案与客户信息
                	for (DataGtgsh wgSh : shList) {
                		String relationId = GetOnlyId.getOnlyId();
                		DataUserTaskRelation userTaskRelationVO = new DataUserTaskRelation();
    					userTaskRelationVO.setId(relationId);
    					userTaskRelationVO.setIdCard(wgSh.getZjhm());
    					userTaskRelationVO.setOrgId(wgSh.getOrgId());
    					userTaskRelationVO.setUserId(wgSh.getUserId());
    					userTaskRelationVO.setCusName(wgSh.getShmt());
    					userTaskRelationVO.setTaskId(taskId);
    					userTaskRelationVO.setType("2");
    					userTaskRelationVO.setBeginDate(DateUtilss.beginOfDate(dataGtgsh.getBeginDate()));
    					userTaskRelationVO.setEndDate(DateUtilss.endOfDate(dataGtgsh.getEndDate()));
    					userTaskRelationVO.setStatus("1");
    					userTaskRelationVO.setComplete("0");
    					userTaskRelationVO.setCreateTime(DateUtils.getNowDate());
    					relationList.add(userTaskRelationVO);
    					if (relationList.size() == 1000) {
    						dataUserTaskRelationMapper.insertDataUserTaskRelationList(relationList);
    	                	relationList = new ArrayList<DataUserTaskRelation>();
    	                	//1000条数据提交一次
    					}
    				}
                	
                	if (relationList.size() > 0) {
                		dataUserTaskRelationMapper.insertDataUserTaskRelationList(relationList);
	                	relationList = new ArrayList<DataUserTaskRelation>();
					}
                	
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        
		return AjaxResult.success();
	}

	@Override
	public List<DataWgShsurvey> wgshAppBiYear(LoginUser userInfo) {

		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String twoYearAgo = DateUtilss.getLastYear(new Date(), -2);// 俩年前
		String ontYearAgo = DateUtilss.getLastYear(new Date(), -1);// 一年前
		String today = formatter.format(date).toString();
		List<DataWgShsurvey> list = dataGtgshMapper.wgshAppBiYear(twoYearAgo, ontYearAgo, today, orgId, userId);

		return list;
	}

	@Override
	public List<DataWgShsurvey> wgshAppBiJd(LoginUser userInfo) {
		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String fjd = DateUtilss.getLastDay(new Date(), -9);// 4季度
		String sjd = DateUtilss.getLastDay(new Date(), -6);// 3季度
		String tjd = DateUtilss.getLastDay(new Date(), -4);// 2季度 二季度减四
		String jd = DateUtilss.getLastDay(new Date(), 0);// 1季度
		String today = formatter.format(date).toString();
		List<DataWgShsurvey> list = dataGtgshMapper.wgshAppBiJd(fjd, sjd, tjd, jd, today, orgId, userId);
		return list;
	}

	@Override
	public List<DataWgShsurvey> wgshAppBiMonth(LoginUser userInfo) {
		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		/**
		 * 获取今年内每个月最后一天
		 */
		ArrayList<String> sjrqs = DateUtilss.getEndMonthByYear(1, 0);

		List<DataWgShsurvey> list = dataGtgshMapper.wgshAppBiMonth(sjrqs, orgId, userId);
		return list;
	}
	
	/**
	 * 查询网格商户
	 * 
	 * @param id 网格商户ID
	 * @return 网格商户
	 */
	@Override
	public DataGtgsh selectDataGtgshById(String id) {
		return dataGtgshMapper.selectDataGtgshById(id);
	}

	/**
	 * 查询网格商户列表
	 * 
	 * @param dataGtgsh 网格商户
	 * @return 网格商户
	 */
	@Override
	public List<DataGtgsh> selectDataGtgshList(DataGtgsh dataGtgsh) {
		return dataGtgshMapper.selectDataGtgshList(dataGtgsh);
	}



	/**
	 * 批量删除网格商户
	 * 
	 * @param ids 需要删除的网格商户ID
	 * @return 结果
	 */
	@Override
	public int deleteDataGtgshByIds(String[] ids) {
		return dataGtgshMapper.deleteDataGtgshByIds(ids);
	}

	/**
	 * 删除网格商户信息
	 * 
	 * @param id 网格商户ID
	 * @return 结果
	 */
	@Override
	public int deleteDataGtgshById(String id) {
		return dataGtgshMapper.deleteDataGtgshById(id);
	}
	private void addUpdateRecord(DataGtgsh old,DataGtgsh gtgsh)
    {
        List<Map<String, Object>> list = new ArrayList<>();

        String record = GetOnlyId.getOnlyId();
        if(this.checkValueChange(old.getZjhm(),gtgsh.getZjhm()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard", gtgsh.getZjhm());
            map.put("attribute", "证件号码");
            map.put("oldValue", old.getZjhm());
            map.put("newValue", gtgsh.getZjhm());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
            //当修改证件号码，必须将修改记录表中原来的证件号码更新为新的证件号码
          //将变更记录表中的身份证改成最新的
			dataCustomerRecordMapper.upRecoro(old.getZjhm(),gtgsh.getZjhm());
            old.setZjhm(gtgsh.getZjhm());
        }
        if(this.checkValueChange(old.getShmt(),gtgsh.getShmt()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","商户门头");
            map.put("oldValue", old.getShmt());
            map.put("newValue", gtgsh.getShmt());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getShzcmc(),gtgsh.getShzcmc()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","商户注册名称");
            map.put("oldValue", old.getShzcmc());
            map.put("newValue", gtgsh.getShzcmc());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getJyfw(),gtgsh.getJyfw()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","经营范围");
            map.put("oldValue", old.getJyfw());
            map.put("newValue", gtgsh.getJyfw());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getJydz(),gtgsh.getJydz()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","经营地址");
            map.put("oldValue", old.getJydz());
            map.put("newValue", gtgsh.getJydz());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getFddbr(),gtgsh.getFddbr()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","法定代表人");
            map.put("oldValue", old.getFddbr());
            map.put("newValue", gtgsh.getFddbr());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getFddbrzjhm(),gtgsh.getFddbrzjhm()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","法定代表人证件号码");
            map.put("oldValue", old.getFddbrzjhm());
            map.put("newValue", gtgsh.getFddbrzjhm());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getSjjyr(),gtgsh.getSjjyr()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","实际经营人");
            map.put("oldValue", old.getSjjyr());
            map.put("newValue", gtgsh.getSjjyr());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getSjjyrzjhm(),gtgsh.getSjjyrzjhm()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","实际经营人证件号码");
            map.put("oldValue", old.getSjjyrzjhm());
            map.put("newValue", gtgsh.getSjjyrzjhm());
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
        if(this.checkValueChange(old.getJyzk(),gtgsh.getJyzk()))
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("record", record);
            map.put("type", "2");
            map.put("idCard",old.getZjhm());
            map.put("attribute","经营状况");
            map.put("oldValue", "0".equals(old.getJyzk())?"不正常":"正常");
            map.put("newValue", "0".equals(gtgsh.getJyzk())?"不正常":"正常");
            map.put("userId", gtgsh.getUpdateUser());// 从当前客户经理登陆信息中获取
            map.put("id", GetOnlyId.getOnlyId());
            list.add(map);
        }
		if(!StringUtils.isEmpty(list))
		{
			for(Map<String, Object> map :list ) {
				System.err.println(map.toString());
				dataCustomerRecordMapper.insertCustRecord(map);// DATA_CUSTOMER_RECORD
			}
		}
    }
	
    private boolean checkValueChange(String oldV,String newV)
    {
        if(StringUtils.isEmpty(newV))
        {
            return false;
        }
        if (StringUtils.isEmpty(oldV) && StringUtils.isEmpty(newV))
        {
            //两个值都为空，表示没有变更，不做记录
            return false;
        }
        else if (StringUtils.isEmpty(oldV) && !newV.equals(oldV))
        {
            //原来的值为空，且不等于修改后的值
            return true;
        }else if (StringUtils.isEmpty(newV) && !oldV.equals(newV))
        {
            //修改后的值为空，但原来的值不为空
            return true;
        }
        else
        {
            return !newV.equals(oldV);
        }
    }
}
